package com.tadpole.music.adapter.base;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.viewpager.widget.PagerAdapter;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.tadpole.music.R;
import com.tadpole.music.utils.largeImage.InputStreamBitmapDecoderFactory;
import com.tadpole.music.utils.largeImage.LargeImageView;


import java.io.File;
import java.io.FileInputStream;
import java.util.List;


/**
 * Created by JiYaRuo on 2018/9/12.
 * 邻居圈图片展示ViewPager的适配器
 */

public class NineGridViewVpAdapter extends PagerAdapter {
    private boolean imgBigPicLocal;
    private boolean imgBigPicLocalCapacity;
    private Activity mContext;
    private List<String> mData;
    private NineGridViewItemCLickListener nineGridViewItemCLickListener;

    public NineGridViewVpAdapter(Activity context, List<String> list, NineGridViewItemCLickListener mNineGridViewItemCLickListener) {
        mContext = context;
        mData = list;
        this.nineGridViewItemCLickListener = mNineGridViewItemCLickListener;
    }

    public NineGridViewVpAdapter(Activity context, List<String> list, NineGridViewItemCLickListener mNineGridViewItemCLickListener, boolean imgBigPicLocal, boolean imgBigPicLocalCapacity) {
        mContext = context;
        mData = list;
        this.nineGridViewItemCLickListener = mNineGridViewItemCLickListener;
        this.imgBigPicLocal = imgBigPicLocal;
        this.imgBigPicLocalCapacity = imgBigPicLocalCapacity;
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @SuppressLint("CheckResult")
    @Override
    public Object instantiateItem(final ViewGroup container, final int position) {
        View view = View.inflate(mContext, R.layout.neighbour_big_pic_vp_item, null);
        final LargeImageView largeImageView = (LargeImageView) view.findViewById(R.id.liv_big_pic_vp_item);
        final RelativeLayout rlDialog = (RelativeLayout) view.findViewById(R.id.rl_dialog);
        final TextView tvLoadDialog = (TextView) view.findViewById(R.id.tv_load_dialog);
//        Glide.with(mContext).load(mData.get(position)).downloadOnly(new SimpleTarget<File>() {
//
//            @Override
//            public void onResourceReady(final File resource, Transition<? super File> transition) {
//
//                rlDialog.setVisibility(View.GONE);
//                try {
//                //主线程
//                largeImageView.setImage(new InputStreamBitmapDecoderFactory(new FileInputStream(resource)));
//                largeImageView.setVisibility(View.VISIBLE);
//            }catch (Exception e){
//                e.printStackTrace();
//            }
//            }
//        });

        Glide.with(mContext).asBitmap().load((mData.get(position))).into(new SimpleTarget<Bitmap>() {
            @Override
            public void onResourceReady(@NonNull Bitmap bitmap, @Nullable Transition<? super Bitmap> transition) {
                rlDialog.setVisibility(View.GONE);
                try {
                    //主线程
                    largeImageView.setImage( bitmapRotation(bitmap,90));
                    largeImageView.setVisibility(View.VISIBLE);

                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        });



        largeImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                nineGridViewItemCLickListener.ItemClick();
            }
        });
        largeImageView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                nineGridViewItemCLickListener.ItemLongClick(mData.get(position));
                return false;
            }
        });
        container.addView(view);
        return view;
    }


    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        // super.destroyItem(container,position,object); 这一句要删除，否则报错
        container.removeView((View) object);
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    public interface NineGridViewItemCLickListener {
        void ItemClick();

        void ItemLongClick(String itemPicAddress);
    }


    public Bitmap bitmapRotation(Bitmap bm, final int orientationDegree) {

        Matrix m = new Matrix();
        m.setRotate(orientationDegree, (float) bm.getWidth() / 3,
                (float) bm.getHeight() / 3);
        float targetX, targetY;
        if (orientationDegree == 90) {
            targetX = bm.getHeight();
            targetY = 0;
        } else if (orientationDegree == 270) {
            targetX = 0;
            targetY = bm.getWidth();
        } else {
            targetX = bm.getHeight();
            targetY = bm.getWidth();
        }

        final float[] values = new float[9];
        m.getValues(values);

        float x1 = values[Matrix.MTRANS_X];
        float y1 = values[Matrix.MTRANS_Y];

        m.postTranslate(targetX - x1, targetY - y1);

        Bitmap bm1 = Bitmap.createBitmap(bm.getHeight(), bm.getWidth(),
                Bitmap.Config.ARGB_8888);

        Paint paint = new Paint();
        Canvas canvas = new Canvas(bm1);
        canvas.drawBitmap(bm, m, paint);

        return bm1;
    }
}
